package com.bawei.day15_service2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

/**
 服务的2大应用场景：音乐播放器+下载文件（后台下载文件 IntentService）





 */
public class Main2Activity extends AppCompatActivity {
    private MyRecevicer myRecevicer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //开启服务 无需停止 会自动停止
        Intent intent = new Intent(this,MyItentService.class);
        startService(intent);
        //注册广播
        myRecevicer = new MyRecevicer();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.bawei.net");
        registerReceiver(myRecevicer,intentFilter);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myRecevicer);
    }
    class MyRecevicer extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if("com.bawei.net".equals(intent.getAction())){
                Bundle bundle = intent.getExtras();
                String result = bundle.getString("result");
                Toast.makeText(context, ""+result, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
